#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: main.py
@time: 2017/12/20 14:59
"""
from multiprocessing import Process

import click

from config.beanstalk_conf import BEANSTALK_CONF
from logger import Logger
from worker.annual_worker import AnnualWorker

global_log = Logger('gsxt_annual_crawl.log').get_logger()


# 获得子进程日志名称
def get_process_log_name(process_id):
    return 'annual-worker-{}.log'.format(process_id)


# 进程启动入口
def process_start(thread_num, input_tube, store_tube, process_id):
    log_name = get_process_log_name(process_id)
    log = Logger(log_name).get_logger()
    log.info("当前进程: process_id = {}".format(process_id))
    log.info("当前进程协程数目: thread_num = {}".format(thread_num))
    log.info("当前进程年报抓取请求消息队列: tube = {}".format(input_tube))
    log.info("当前进程存储消息队列: tube = {}".format(store_tube))
    detail_worker = AnnualWorker(thread_num, input_tube, store_tube, process_id, BEANSTALK_CONF, log=log)
    detail_worker.start()


@click.command()
@click.option('--process_num',
              default=1,
              type=int,
              help='进程数目')
@click.option('--thread_num',
              default=1,
              type=int,
              help='协程数目')
@click.option('--input_tube',
              default='gsxt_annual_download',
              help='接收年报抓取请求')
@click.option('--store_tube',
              default='gsxt_store',
              help='存储消息队列')
def main(process_num, thread_num, input_tube, store_tube):
    process_list = []

    for process_id in xrange(process_num):
        process_handler = Process(target=process_start,
                                  args=(thread_num, input_tube, store_tube, process_id,))
        process_handler.start()
        process_list.append(process_handler)

    global_log.info("年报抓取进程组初始化完成...")
    for _process in process_list:
        _process.join()


if __name__ == '__main__':
    main()
